package com.nidu.demo.dict.executor.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.dict.convertor.DictDataCriteriaConvertor;
import com.nidu.demo.dict.dto.DictDataListQry;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.dict.convertor.DictDataConvertor;
import com.nidu.demo.dict.dto.DictDataCO;
import com.nidu.demo.dict.gateway.DictDataGateway;
import com.nidu.demo.dict.model.DictData;
import com.nidu.demo.dict.model.DictDataCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 字典类型列表查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DictDataListQryExe {

    private final DictDataGateway gateway;

    private final DictDataConvertor convertor;

    private final DictDataCriteriaConvertor criteriaConvertor;

    public MultiResponse<DictDataCO> execute(DictDataListQry qry) {
        DictDataCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<DictData> dictDataList = gateway.listByCondition(criteria);
        if(CollUtil.isEmpty(dictDataList)){
            throw new BizException(ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getCode(), ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getMessage());
        }
        List<DictDataCO> companyCOS = convertor.toClientObjects(dictDataList);
        return MultiResponse.of(companyCOS);
    }

}